package com.tan.androidtest.activity;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.tan.androidtest.R;
import com.tan.androidtest.base.BaseActivity;

/**
 * 测试Handler Looper MessageQueue
 */

public class TestHandlerLoopMessageQueueActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvSend;
    private TextView mTvShow;

    @Override
    public int initLayoutId() {
        return R.layout.activity_test_handler_looper_messagequeue_layout;
    }

    @Override
    public void initTitle(TextView tv_bar_left, TextView tv_bar_title, TextView tv_bar_right) {
        tv_bar_title.setText("测试Handler Looper MessageQueue");
    }

    @Override
    public void initView() {
        mTvSend = findViewById(R.id.tv_send);
        mTvShow = findViewById(R.id.tv_show);
        mTvSend.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_send: {
                testHandlerThread();
                break;
            }
        }
    }

    //HandlerThread  相当于在子线程中处理发过来的消息,其内部帮我们调用了Looper.prepare和Looper.loop
    private void testHandlerThread() {
        final HandlerThread myHandlerThread = new HandlerThread("myHandlerThread");
        myHandlerThread.start();
        final Handler myHandler = new Handler(myHandlerThread.getLooper(), new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 100) {
                    Log.i("==--", msg.arg1 + ":------------------aaaaaaaa");
                } else if (msg.what == 102) {
                    Log.i("==--", msg.arg1 + ":------------------aaaaaaaa102");
                } else if (msg.what == 103) {
                    Log.i("==--", msg.arg1 + ":------------------aaaaaaaa103");
                }
                return false;
            }
        });

        new Thread() {
            @Override
            public void run() {
                Message message = myHandler.obtainMessage();
                message.what = 100;
                message.arg1 = (int) Thread.currentThread().getId();
                myHandler.sendMessage(message);
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                Message message = myHandler.obtainMessage();
                message.what = 102;
                message.arg1 = (int) Thread.currentThread().getId();
                myHandler.sendMessage(message);
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                Message message = myHandler.obtainMessage();
                message.what = 103;
                message.arg1 = (int) Thread.currentThread().getId();
                myHandler.sendMessage(message);
            }
        }.start();


    }


}
